//
//  ShoppingCartViewController.m
//  shoppingMall
//
//  Created by Bear on 2016/10/27.
//  Copyright © 2016年 dragontrail. All rights reserved.
//

#import "ShoppingCartViewController.h"
#import "GoodsModel.h"
#import "GoodsCell.h"
#import "UIButton+CustomStyle.h"
#import "AccountsView.h"

#define GOODSCELLID @"GoodsCell"
#define SCREEN_WIDTH ([UIScreen mainScreen].bounds.size.width)
#define SCREEN_HEIGHT ([UIScreen mainScreen].bounds.size.height)


@interface ShoppingCartViewController ()<UITableViewDelegate,UITableViewDataSource>


@property(nonatomic ,retain)UITableView* tableView;
#warning consider this question！！！
@property(nonatomic, retain)NSArray<NSArray *>* goodsArrbCategoryByStore;


@property(nonatomic, retain)NSArray* storeNameArr;


@end

@implementation ShoppingCartViewController

-(UITableView *)tableView{

    if (!
        _tableView) {
        _tableView = [[UITableView alloc] initWithFrame:self.view.frame style:UITableViewStyleGrouped];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;

    }
    return _tableView;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.title = @"购物车";
    self.storeNameArr  = [self.goodsStoreDictionary allKeys];
    [self.view addSubview:self.tableView];
    [self.tableView registerNib:[UINib nibWithNibName:@"GoodsCell" bundle:nil] forCellReuseIdentifier:GOODSCELLID];
    AccountsView* accountView = [[AccountsView alloc] initWithFrame:CGRectMake(0, SCREEN_HEIGHT-44-50, SCREEN_WIDTH, 50)];
    accountView.selectAllActionBlock = ^(UIButton* sender){
        sender.selected = !sender.selected;
        [self selectAllCellWithSelect:sender.selected];
        [self.tableView reloadData];
    };

    accountView.accoutsAllActionBlock = ^(UIButton* sender){
        //结算的代码放到这里

    };
    [self.view addSubview:accountView];
}

-(void)selectAllCellWithSelect:(BOOL)select{

    
    for (int i = 0; i<self.storeNameArr.count; i++) {
        NSString* storeName = [self.storeNameArr objectAtIndex:i];
        NSArray* goodsArrbCategoryByStore = [self.goodsStoreDictionary objectForKey:storeName];
        for (int j = 0; j< goodsArrbCategoryByStore.count; j++) {
            GoodsModel* model = [goodsArrbCategoryByStore objectAtIndex:j];
            if (model.isSelected != select) {
                model.isSelected = select;
            }
        };
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{

    GoodsCell* cell = [tableView dequeueReusableCellWithIdentifier:GOODSCELLID forIndexPath:indexPath];
    [cell setSelectionStyle:UITableViewCellSelectionStyleNone];
    NSArray* goodArr = [self.goodsStoreDictionary objectForKey:[[self.goodsStoreDictionary allKeys] objectAtIndex:indexPath.section]];
    GoodsModel* model = [goodArr objectAtIndex:indexPath.row];
    [cell setCellwithModel:model];
    if (model.isEdit) {
        [cell ChangeSytleWithStyle:CellEditStyle];
    }else{
        [cell ChangeSytleWithStyle:CellNoEditSyle];
    }
    return cell;
}


-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{

    return self.goodsStoreDictionary?self.goodsStoreDictionary.count:0;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{

    NSArray* sourceArr = [self.goodsStoreDictionary objectForKey:[self.storeNameArr objectAtIndex:section]];
    return sourceArr.count;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{

    return 120;
}

//头视图得加入重用机制才可以
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    UITableViewHeaderFooterView* headFootView = [tableView dequeueReusableHeaderFooterViewWithIdentifier:@"HeadView"];
    if (!headFootView) {
        headFootView =  [[UITableViewHeaderFooterView alloc] initWithReuseIdentifier:@"HeadView"];
        UIView* headView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 40)];
        UIButton* select_button = [UIButton buttonWithType:UIButtonTypeSystem];
        [select_button goodsSelectStyle];
        [select_button setFrame:CGRectMake(5, 5, 30, 30)];
        [select_button setTag:section+2000];
        [select_button addTarget:self action:@selector(selectGoodsAtStore:) forControlEvents:UIControlEventTouchUpInside];
        UILabel* storeTitleLable = [[UILabel alloc] initWithFrame:CGRectMake(40, 5,SCREEN_WIDTH/2 , 30)];
        storeTitleLable.text = [[self.goodsStoreDictionary allKeys] objectAtIndex:section];
        UIButton* edit_button = [UIButton buttonWithType:UIButtonTypeCustom];
        [edit_button setTitle:@"编辑" forState:UIControlStateNormal];
        [edit_button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        [edit_button setTitleColor:[UIColor blackColor] forState:UIControlStateSelected];
        [edit_button setTitle:@"完成" forState:UIControlStateSelected];
        [edit_button setFrame:CGRectMake(SCREEN_WIDTH - 45, 5, 40, 30)];
        [edit_button setTag:section+1000];
        [edit_button addTarget:self action:@selector(editActionOfGoods:) forControlEvents:UIControlEventTouchUpInside];
        [headView addSubview:select_button];
        [headView addSubview:storeTitleLable];
        [headView addSubview:edit_button];
        [headView setBackgroundColor:[UIColor whiteColor]];
        [headFootView addSubview:headView];
    }
    return headFootView;
}



-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 40;
}

-(CGFloat)tableView:(UITableView *)tableView estimatedHeightForHeaderInSection:(NSInteger)section{

    return 40;
}


//商店内选择商品的方法
-(void)selectGoodsAtStore:(UIButton*)sender{
    NSLog(@"我点击了选择按钮");
    sender.selected = !sender.selected;

    NSArray* arr = [self.goodsStoreDictionary objectForKey:[self.storeNameArr objectAtIndex:sender.tag-2000]];
    for (int i = 0; i<arr.count; i++) {
        GoodsModel* model = [arr objectAtIndex:i];
        model.isSelected = sender.selected;
    }
    [self.tableView reloadData];

}

//编辑商品的方法
-(void)editActionOfGoods:(UIButton*)sender{
    NSLog(@"%s",__func__);
    sender.selected = !sender.selected;
    NSArray* arr = [self.goodsStoreDictionary objectForKey:[self.storeNameArr objectAtIndex:sender.tag-1000]];
    for (int i = 0; i<arr.count; i++) {
        GoodsModel* model = [arr objectAtIndex:i];
        model.isEdit = sender.selected;
    }
    [self.tableView reloadData];

}


/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
